"use strict";
cc._RF.push(module, 'bba94KbTZZEO5FMYApschDF', 'HelperMgr');
// script/libs/HelperMgr.ts

"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var Constants_1 = require("./Constants");
var Helper = /** @class */ (function () {
    function Helper() {
    }
    /**
     *
     * @returns
     */
    Helper.randomBoolen = function () {
        var boolean = Math.random() >= 0.5;
        return boolean;
    };
    /**
     * 获取两点间距离
     * @param p1X
     * @param p1Y
     * @param p2X
     * @param p2Y
     * @returns {number}
     */
    Helper.getDistance = function (p1X, p1Y, p2X, p2Y) {
        var disX = p2X - p1X;
        var disY = p2Y - p1Y;
        var disQ = disX * disX + disY * disY;
        return Math.sqrt(disQ);
    };
    /**
    * 获取两点间角度
    * @param p1X
    * @param p1Y
    * @param p2X
    * @param p2Y
    * @returns {number}
    */
    Helper.getAngele = function (p1X, p1Y, p2X, p2Y) {
        var disX = p2X - p1X;
        var disY = p2Y - p1Y;
        var angle = Math.atan2(disX, disY) / Math.PI * 180;
        return angle;
    };
    /**
  * 获取两点间弧度
  * @param p1X
  * @param p1Y
  * @param p2X
  * @param p2Y
  * @returns {number}
  */
    Helper.getRadian = function (p1X, p1Y, p2X, p2Y) {
        var disX = p2X - p1X;
        var disY = p2Y - p1Y;
        var radian = Math.atan2(disX, disY) - Math.PI / 2;
        return radian;
    };
    /**
     * 随机a-b之间某个数 b大于a
     * @param a
     * @param b
     * @param isInteger 是否取整
     */
    Helper.randAtoB = function (a, b, isInteger) {
        if (isInteger === void 0) { isInteger = true; }
        var rand = Math.random() * (b - a) + a;
        return isInteger ? Math.floor(rand) : rand;
    };
    /**
     * 获取排名
     * @param score
     * @param scores
     */
    Helper.getScoreRank = function (score, scores) {
        var rank = 0;
        while (score < scores[rank] && rank < scores.length) {
            rank++;
        }
        return rank + 1;
    };
    /**
     * 累加数据
     * @param target
     */
    Helper.accumulationNumber = function (target) {
        if (target % 2 == 0) {
            return (1 + target) * (target / 2);
        }
        else {
            return (1 + target) * (Math.floor(target / 2)) + Math.ceil(target / 2);
        }
    };
    /**判断物体是否超出地图外 */
    Helper.isOutMap = function (target) {
        return target.x > Constants_1.default.MapSize.x / 2 || target.x < -Constants_1.default.MapSize.x / 2 || target.y > Constants_1.default.MapSize.y / 2 || target.y < -Constants_1.default.MapSize.y / 2;
    };
    /**等待几秒后继续执行 */
    Helper.waitForSec = function (sec) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (res, rej) {
                        setTimeout(function () {
                            res();
                        }, sec * 1000);
                    })];
            });
        });
    };
    /**
       * 线性差值
       * @param value1
       * @param value2
       * @param amount
       */
    Helper.lerp = function (value1, value2, amount) {
        amount = amount < 0 ? 0 : amount;
        amount = amount > 1 ? 1 : amount;
        return value1 + (value2 - value1) * amount;
    };
    /**展示界面 */
    Helper.showUI = function (name) {
        var path = "ui/" + name;
        cc.loader.loadRes(path, function (err, prefab) {
            var node = cc.instantiate(prefab);
            cc.director.getScene().getChildByName('Canvas').addChild(node);
        });
    };
    /**展示提示界面 */
    Helper.showTip = function (tip) {
        var path = "ui/UITip";
        cc.loader.loadRes(path, function (err, prefab) {
            var node = cc.instantiate(prefab);
            cc.director.getScene().getChildByName('Canvas').addChild(node);
            node.getComponent("UITip").content = tip;
        });
    };
    /**
       * 裁切字符串 并返回number
       * @param text
       * @param separator
       */
    Helper.splitToNumber = function (text, separator) {
        if (!text || text.length == 0) {
            return [];
        }
        var strArr = text.split(separator);
        var newArr = [];
        for (var i = 0; i < strArr.length; i++) {
            newArr.push(Number(strArr[i]));
        }
        return newArr;
    };
    /**
    * 加载resources资源
    * @param url
    * @param type
    */
    Helper.loadRes = function (url, type) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (res, rej) {
                        cc.loader.loadRes(url, type, function (err, assets) {
                            if (err) {
                                console.log('资源错误', url, err);
                                rej(err);
                            }
                            else {
                                res(assets);
                            }
                        });
                    })];
            });
        });
    };
    /**
    * 加载bundle资源
    * @param url
    * @param type
    */
    Helper.loadBundle = function (url, type) {
        return __awaiter(this, void 0, void 0, function () {
            return __generator(this, function (_a) {
                return [2 /*return*/, new Promise(function (res, rej) {
                        cc.assetManager.loadBundle('animation', function (err, bundle) {
                            bundle.load(url, type, function (err, spriteFrame) {
                                if (err) {
                                    console.log('资源错误', url, err);
                                    rej(err);
                                }
                                else {
                                    res(spriteFrame);
                                }
                            });
                        });
                    })];
            });
        });
    };
    /**
      * 给按钮增加回调
      * @param node
      * @param btn
      * @param funcName
      * @param className
      */
    Helper.addClickEvent = function (node, btn, funcName, className, customEventData) {
        if (customEventData === void 0) { customEventData = ""; }
        var event = new cc.Component.EventHandler();
        event.target = node;
        event.handler = funcName;
        event.component = className;
        event.customEventData = customEventData;
        btn.clickEvents.push(event);
    };
    /**
     * 判断一个点是否在一个多边形内部
     * @param pt 目标点
     * @param poly 多边形数组
     * @returns
     */
    Helper.PointInPoly = function (pt, poly) {
        for (var c = false, i = -1, l = poly.length, j = l - 1; ++i < l; j = i)
            ((poly[i].y <= pt.y && pt.y < poly[j].y) || (poly[j].y <= pt.y && pt.y < poly[i].y))
                && (pt.x < (poly[j].x - poly[i].x) * (pt.y - poly[i].y) / (poly[j].y - poly[i].y) + poly[i].x)
                && (c = !c);
        return c;
    };
    return Helper;
}());
exports.default = Helper;

cc._RF.pop();